package cn.gaily.eduservice.controller;


import cn.gaily.commonutils.CourseInfoVoOrder;
import cn.gaily.commonutils.R;
import cn.gaily.eduservice.entity.EduCourse;
import cn.gaily.eduservice.entity.vo.CourseInfoVo;
import cn.gaily.eduservice.entity.vo.CoursePublishVo;
import cn.gaily.eduservice.entity.vo.CourseQuery;
import cn.gaily.eduservice.feignclient.VodClient;
import cn.gaily.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-07-05
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    @ApiOperation(value = "添加课程基本信息的方法")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回id是为了页面在返回上一步的时候能查到历史数据
        String courseInfoId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseInfoId",courseInfoId);
    }

    @ApiOperation(value = "获得课程基本信息")
    @GetMapping("/getCourseInfo/{couserId}")
    public R getCourseInfo(@PathVariable String couserId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(couserId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    @ApiOperation(value = "根据id修改课程信息")
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "课程最终信息确认")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getpublishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    @ApiOperation("课程最终发布修改状态")
    @PostMapping("/publishCourse/{id}")
    public R publishCourseStatus(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean update = eduCourseService.updateById(eduCourse);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("查询所有课程列表")
    @GetMapping("/getCourseList")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    @ApiOperation("分页查询课程列表")
    @PostMapping("/pageListCourse/{current}/{limit}")
    public R pageQueryCourse(
            @ApiParam(name = "current",value = "当前页数",required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit){
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        eduCourseService.page(pageCourse, null);
        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("带条件分页查询课程列表")
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageQueryCourseCondition(
            @ApiParam(name = "current",value = "当前页数",required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) CourseQuery courseQuery
    ){
        Page<EduCourse> page = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.like("status",status);
        }
        IPage<EduCourse> pageResult = eduCourseService.page(page, wrapper);
        List<EduCourse> records = pageResult.getRecords();
        long total = pageResult.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        int course = eduCourseService.removeCourse(courseId);
        return course != 0 ? R.ok() : R.error();
    }

    @ApiOperation(value = "获得课程基本信息给订单")
    @GetMapping("/getCourseInfoToOrder/{couserId}")
    public CourseInfoVoOrder getCourseInfoToOrder(@PathVariable String couserId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(couserId);
        CourseInfoVoOrder courseInfoVoOrder = new CourseInfoVoOrder();
        BeanUtils.copyProperties(courseInfoVo,courseInfoVoOrder);
        return courseInfoVoOrder;
    }
}

